home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / sort / Source / sort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  2.3 KB  |  137 lines  |  [TEXT/MPS ]

  1. #include <Quickdraw.h>
  2. #include <Dialogs.h>
  3. #include <Memory.h>
  4. #include <Errors.h>
  5.  
  6. #include "sortWindow.h"
  7. #include "sortMenus.h"
  8. #include "sort.h"
  9. #include "notif.h"
  10. #include "acur.h"
  11.  
  12. #define ALRT_OUT_OF_MEM 132
  13.  
  14. #define MINVAL            1
  15. #define    HILITEDELAYVAL    0L
  16. #define    UNHILITEDELAYVAL 0L
  17.  
  18. long *sortdata = NULL; 
  19. long numdataitems =  50;
  20. unsigned long numcompares = 0, numexchanges = 0;
  21. long mindataval = MINVAL, maxdataval = 500;
  22. long hilitedelay = HILITEDELAYVAL, unhilitedelay = UNHILITEDELAYVAL;
  23. Boolean hilite_exchange = false;
  24. Boolean hilite_compare = false;
  25. static long old_numdata = -1;
  26. Boolean data_sorted;
  27.  
  28.  
  29. OSErr init_data(long maxdata)
  30. {
  31.     long count;
  32.     
  33.     if ((maxdata != old_numdata) && (sortdata != NULL)) {
  34.         DisposPtr((Ptr)sortdata);
  35.         sortdata = NULL;
  36.         data_sorted = true;
  37.     }
  38.     
  39.     if (maxdata != old_numdata) {
  40.         sortdata = (long*) NewPtr(maxdata * sizeof(long));
  41.         data_sorted = true;
  42.     }
  43.  
  44.     if (sortdata == NULL) {
  45.         if (gInBackground) {
  46.             wait_Foreground(1);
  47.         }
  48.         Alert(ALRT_OUT_OF_MEM,NULL);
  49.         return mFulErr;
  50.     }
  51.     old_numdata = maxdata;
  52.     for (count = 0; count < maxdata; count++) {
  53.         if (data_sorted == true) {
  54.             eraseitem(count);
  55.             sortdata[count] = ((Random() + 32768) % (maxdataval - mindataval + 1)) + mindataval;
  56.             drawitem(count);
  57.         }
  58.     }
  59.     data_sorted = false;
  60.     return noErr;
  61. }
  62.  
  63.  
  64. short compare(long ino1, long ino2, long i1, long i2)
  65. {
  66. //    HandleEvent();
  67.     SpinCursor();
  68.  
  69.     if (hilite_compare) {
  70.         myHilite(ino1);
  71.         myHilite(ino2);
  72.         do_hilitedelay();
  73.  
  74.         unhilite(ino1);
  75.         unhilite(ino2);
  76.         do_unhilitedelay();
  77.     }
  78.     numcompares++;
  79.  
  80.     if (i1 > i2)
  81.         return 1;
  82.     if (i1 < i2)
  83.         return -1;
  84.     return 0;
  85. }
  86.  
  87. void swap(long item1, long item2, long* pitem1, long* pitem2)
  88. {
  89.     long temp;
  90.  
  91. //    HandleEvent();
  92.     SpinCursor();
  93.  
  94.     if (hilite_exchange) {
  95.         myHilite(item1);
  96.         myHilite(item2);
  97.         do_hilitedelay();
  98.     }
  99.  
  100.     eraseitem(item1);
  101.     eraseitem(item2);
  102.  
  103.     temp = *pitem1;
  104.     *pitem1 = *pitem2;
  105.     *pitem2 = temp;
  106.  
  107.     if (hilite_exchange) {
  108.         myHilite(item1);
  109.         myHilite(item2);
  110.  
  111.         do_hilitedelay();
  112.  
  113.         unhilite(item1);
  114.         unhilite(item2);
  115.         do_unhilitedelay();
  116.     }
  117.     else {
  118.         drawitem(item1);
  119.         drawitem(item2);
  120.     }
  121.     numexchanges++;
  122. }
  123.  
  124.  
  125. pascal void FrameUserProc(DialogPtr dialog, short itemNo)
  126. {
  127.     auto    short        itemType;
  128.     auto    Handle    itemHandle;
  129.     auto    Rect        itemRect;
  130.     
  131.     SetPort(dialog);
  132.     GetDItem(dialog, itemNo, &itemType, &itemHandle, &itemRect);
  133.     PenSize(3,3);
  134.     FrameRoundRect(&itemRect, 17, 17);
  135. }
  136.  
  137.